package com.tjtt.gwm.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.*;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.flow.bean.FlowTaskInfo;
import com.tjtt.gwm.service.EquipmentOutService;
import com.tjtt.tdm.flow.bean.FlowTask;
import com.tjtt.gwm.bean.Outstoreform;
import com.tjtt.gwm.bean.Formrelateequ;
import com.tjtt.gwm.bean.EquipmentParts;
import com.tjtt.gwm.service.EquipmentPartsService;
import com.tjtt.gwm.bean.EquipmentOutIndex;


public class EquipmentOutAction extends BaseAction {
	
	private EquipmentOutService equipmentOutService;
	private Outstoreform outstoreform;
	private Formrelateequ  formrelateequ;
	private EquipmentPartsService equipmentPartsService;
	
	
	public EquipmentPartsService getEquipmentPartsService() {
		return equipmentPartsService;
	}

	public void setEquipmentPartsService(EquipmentPartsService equipmentPartsService) {
		this.equipmentPartsService = equipmentPartsService;
	}

	public Formrelateequ getFormrelateequ() {
		return formrelateequ;
	}

	public void setFormrelateequ(Formrelateequ formrelateequ) {
		this.formrelateequ = formrelateequ;
	}

	public EquipmentOutService getEquipmentOutService() {
		return equipmentOutService;
	}

	public void setEquipmentOutService(EquipmentOutService equipmentOutService) {
		this.equipmentOutService = equipmentOutService;
	}
	
	public Outstoreform getOutstoreform() {
		return outstoreform;
	}

	public void setOutstoreform(Outstoreform outstoreform) {
		this.outstoreform = outstoreform;
	}

	/**
	 * 备件申请人 申请发起逻辑处理。跳转到备件出库发起页面
	 * 页面url传参 flowid 、equipmentid
	 * @return String
	 * @author gw00082425
	 */
	public String  querySelectedEquipment()
	{
		String ids = getRequest().getParameter("equipmentid"); //出库备件id	
		//String  applyPerson = this.getUserName();
		String  applyPerson = this.getUserNM();
	    String  applyPersonId = this.getUserId();// 获得登录名及id
	    getRequest().setAttribute("applyid", applyPersonId);
    	//获得当前日期   String 
	    Date now = new Date(); 
	    DateFormat d2 = DateFormat.getDateInstance(); 
	    String timeStr = d2.format(now); 
	    getRequest().setAttribute("timeStr", timeStr);
	    
	    //查询EQUIPMENT_OUT_INDEX 生成 出库单号；	    
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String nowdate = sdf.format(new Date());    
        String outSerial = "";
        EquipmentOutIndex eoi = new EquipmentOutIndex();
        List<EquipmentOutIndex> list = new ArrayList <EquipmentOutIndex>();
        list = equipmentOutService.queryEquoutIndxByDate(nowdate);
	    if(list.size()==0){
	    	eoi.setEoYear(nowdate);
	    	eoi.setEoIndex("1");
	    	int i = 1;
	    	NumberFormat nf = NumberFormat.getInstance();
	        //设置是否使用分组
	        nf.setGroupingUsed(false);
	        //设置最大整数位数
	        nf.setMaximumIntegerDigits(4);
	        //设置最小整数位数   
	        nf.setMinimumIntegerDigits(4);
	        //输出测试语句  		    
	    	outSerial = "CY-CK-"+nowdate+"-"+nf.format(i);	    	
	    	equipmentOutService.saveToEquoutIndx(eoi);
	    }
	    else{
	    	String index = ((EquipmentOutIndex)list.get(0)).getEoIndex();
	    	int n = Integer.parseInt(index);   	
	    	NumberFormat nf = NumberFormat.getInstance();
	        //设置是否使用分组
	        nf.setGroupingUsed(false);
	        //设置最大整数位数
	        nf.setMaximumIntegerDigits(4);
	        //设置最小整数位数   
	        nf.setMinimumIntegerDigits(4);
	        //输出测试语句  		    
	    	outSerial = "CY-CK-"+nowdate+"-"+ nf.format(++n);
	    	String  indexNew = Integer.toString(n);
	    	((EquipmentOutIndex)list.get(0)).setEoIndex(indexNew);
	    	equipmentOutService.updateEquipmentOutIndex((EquipmentOutIndex)list.get(0));
	    }    
	    getRequest().setAttribute("outSerial", outSerial);
		getRequest().setAttribute("applyPerson", applyPerson);// 流程发起人为登录用户
		String applyDep = this.getDeptName();
		getRequest().setAttribute("applyDep", applyDep); //申请部门
		// 将ids传递到跳转页面
		getRequest().setAttribute("ids", ids);
		super.initFlowaddBase();
		return "starts";  //跳转到了流程发起表单页面
	}
	
	
	
	public String responPerson(){
		String mainid = getRequest().getParameter("mainid");
		getRequest().setAttribute("mainid", mainid);
		
		
		//String  AuditPerson = this.getUserName(); 
		String  AuditPerson = this.getUserNM(); 
	    String  AuditPersonId = this.getUserId();
	    
	    getRequest().setAttribute("AuditPerson", AuditPerson);
    	//获得日期    String 
	    Date now = new Date(); 
	    DateFormat d2 = DateFormat.getDateInstance(); 
	    String timeStr = d2.format(now); 
	    getRequest().setAttribute("timeStr", timeStr);
		
		
		
	//	由mainid 查询基本表单信息 然后setAttribute
		Outstoreform fm = new Outstoreform();
        fm = equipmentOutService.queryOutstoreFormById(mainid);
	    
	    String applyData   = fm.getApplydate();
	    getRequest().setAttribute("applyData", applyData);
	    
	    String  applyDept  = fm.getApplydept();
	    getRequest().setAttribute("applyDept", applyDept);
	    
	    String applyPerson = fm.getApplyperson();	    
	    getRequest().setAttribute("applyPerson", applyPerson);
	    
	    String formSerial = fm.getFormserial();
	    getRequest().setAttribute("formSerial", formSerial);
	    
	   	String outType = fm.getOuttype();
	   	getRequest().setAttribute("outType", outType);
	     
	   	String outReason = fm.getReasonforout();
	   	getRequest().setAttribute("outReason", outReason);
		
	   	String remarks = fm.getRemarks();
	   	getRequest().setAttribute("remarks", remarks);			
		getRequest().setAttribute("ids", mainid);	
	    super.initFlowupdateBase();		
	    return "responseperson";	
	}
	/**
	 * 负责科长审批，
	 */
	public void responPersonSubmit(){	
		PrintWriter out = null;	
	
		String person = this.getUserNM(); 
	    String  personId = this.getUserId();//登录人信息
	    String suggestion = getRequest().getParameter("leadersuggestion");
		String times = getRequest().getParameter("leadertime");
		try {
			out = getResponse().getWriter();	
			String mainid = getRequest().getParameter("mainid");
			
			Outstoreform osf = equipmentOutService.queryOutstoreFormById(mainid);
			String steptype = getRequest().getParameter("steptype");// 判断是提交还是退回
			
			osf.setLeadername(person);
			osf.setLeaderid(personId);
			osf.setLeadersuggestion(suggestion);
			osf.setLeadertime(times);
			equipmentOutService.UpdateOutstoreForm(osf);
			
			//退回到发起人
			if(steptype.equals("back1")){	
				// 1.获取发起人 name id 更新 flow_task_info
				String nextpersonid = osf.getApplyid();
				String nextpersonname = osf.getApplyperson();			
				//获取审核意见
				
				// 哪些业务信息需要存到outstoreform 中
				
								
				//super.saveUpdateExtend("outstoreform", mainid, "退回", suggestion);
				osf.setBackflag(BigDecimal.valueOf(1));
				equipmentOutService.UpdateOutstoreForm(osf);
				List<FlowTask> list = new ArrayList <FlowTask>();
	   		    list = equipmentOutService.getFlowtaskById(mainid);  		     
	   		    FlowTaskInfo fti = new FlowTaskInfo();
	   		    
	   		    List<FlowTaskInfo> list1 = new ArrayList <FlowTaskInfo>();
	   		    list1 = equipmentOutService.getFlowTaskInfoById(list.get(0).getId());
	   		    list1.get(0).setNextUsersId(","+nextpersonid+",");
	   		    list1.get(0).setNextUsersName(","+nextpersonname+",");
	   		    fti = list1.get(0);
	   		    equipmentOutService.updateFlowTaskInfo(fti); 
	   		    out.print("{\"result\":\"success\"}");
			    out.flush();
				out.close();

			}
			else{
				// 哪些业务信息需要存到outstoreform 中
								
				//通过的话，下一个节点人信息
				super.saveUpdateBase("TDM_EPINSTORE", mainid);
				String houseKeeper = osf.getHousekeepername();
				String hkId = osf.getHousekeeperid();
				List<FlowTask> list = new ArrayList <FlowTask>();
	   		    list = equipmentOutService.getFlowtaskById(mainid);  		     
	   		    FlowTaskInfo fti = new FlowTaskInfo();   		    
	   		    List<FlowTaskInfo> list1 = new ArrayList <FlowTaskInfo>();
	   		    list1 = equipmentOutService.getFlowTaskInfoById(list.get(0).getId());
	   		    list1.get(0).setNextUsersId(","+hkId+",");
	   		    list1.get(0).setNextUsersName(","+houseKeeper+",");
	   		    fti = list1.get(0);
	   		    equipmentOutService.updateFlowTaskInfo(fti); 			
							
				out.print("{\"result\":\"success\"}");
			    out.flush();
				out.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.print("{\"result\":\"false\"}");
		    out.flush();
			out.close();
		}		
	}
	
	
	public String houseKeeper(){
		
		String mainid = getRequest().getParameter("mainid");
		getRequest().setAttribute("mainid", mainid);
		
		//String  AuditPerson = this.getUserName();
		String  AuditPerson = this.getUserNM(); 
	    String  AuditPersonId = this.getUserId();
	    getRequest().setAttribute("AuditPerson", AuditPerson);
    	//获得日期    String 
	    Date now = new Date(); 
	    DateFormat d2 = DateFormat.getDateInstance(); 
	    String timeStr = d2.format(now); 
	    getRequest().setAttribute("timeStr", timeStr);
		
		Outstoreform fm = new Outstoreform();
        fm = equipmentOutService.queryOutstoreFormById(mainid);
	       
	    String applyData   = fm.getApplydate();
	    getRequest().setAttribute("applyData", applyData);
	    
	    String  applyDept  = fm.getApplydept();
	    getRequest().setAttribute("applyDept", applyDept);
	    
	    String applyPerson = fm.getApplyperson();	    
	    getRequest().setAttribute("applyPerson", applyPerson);
	    
	    String formSerial = fm.getFormserial();
	    getRequest().setAttribute("formSerial", formSerial);
	    
	   	String outType = fm.getOuttype();
	   	getRequest().setAttribute("outType", outType);
	     
	   	String outReason = fm.getReasonforout();
	   	getRequest().setAttribute("outReason", outReason);
		
	   	String remarks = fm.getRemarks();
	   	getRequest().setAttribute("remarks", remarks);
		
		getRequest().setAttribute("ids", mainid);
		
		String leader = fm.getLeadername();
		getRequest().setAttribute("leader", leader);
		
		String leadersuggestion = fm.getLeadersuggestion();
		getRequest().setAttribute("leadersuggestion", leadersuggestion);
		
		String leadertime = fm.getLeadertime();
		getRequest().setAttribute("times", leadertime);
		
		super.initFlowupdateBase();
			
		return "housekeeper";
		
	}
	
	public void houseKeeperSubmit(){
		PrintWriter out = null;	
		String person = this.getUserNM();
	    String  personId = this.getUserId();//登录人信息
		try {
			out = getResponse().getWriter();	
			String mainid = getRequest().getParameter("mainid");
			String suggestion = getRequest().getParameter("warehousekeepersuggestion");
			String times = getRequest().getParameter("warehousekeepertime");
			
			Outstoreform osf = equipmentOutService.queryOutstoreFormById(mainid);
			osf.setHousekeeperid(personId);
			osf.setHousekeepername(person);
			osf.setHousekeepersuggestion(suggestion);
			osf.setHousekeepertime(times);
			equipmentOutService.UpdateOutstoreForm(osf);
			String nextpersonid = osf.getApplyid();
			String nextpersonname = osf.getApplyperson();
			String steptype = getRequest().getParameter("steptype");// 判断是提交还是退回
			//退回到发起人
			if(steptype.equals("back2")){	
				// 1.获取发起人 name id 更新 flow_task_info
							
				//获取审核意见								
				//在outstoreform 中存储审核人及审核意见。
										
				//super.saveUpdateExtend("outstoreform", mainid, "退回", suggestion);
				osf.setBackflag(BigDecimal.valueOf(2));
				equipmentOutService.UpdateOutstoreForm(osf);
				List<FlowTask> list = new ArrayList <FlowTask>();
	   		    list = equipmentOutService.getFlowtaskById(mainid);  		     
	   		    FlowTaskInfo fti = new FlowTaskInfo();
	   		    
	   		    List<FlowTaskInfo> list1 = new ArrayList <FlowTaskInfo>();
	   		    list1 = equipmentOutService.getFlowTaskInfoById(list.get(0).getId());
	   		    list1.get(0).setNextUsersId(","+nextpersonid+",");
	   		    list1.get(0).setNextUsersName(","+nextpersonname+",");
	   		    fti =  list1.get(0);
	   		    equipmentOutService.updateFlowTaskInfo(fti); 
	   		    out.print("{\"result\":\"success\"}");
			    out.flush();
				out.close();
			}
			else{
				
				// 需要在outstoreform 表中存储什么业务信息。
		
				super.saveUpdateBase("TDM_EPINSTORE", mainid);
				List<FlowTask> list = new ArrayList <FlowTask>();
	   		    list = equipmentOutService.getFlowtaskById(mainid);  		     
	   		    FlowTaskInfo fti = new FlowTaskInfo();
	   		    
	   		    List<FlowTaskInfo> list1 = new ArrayList <FlowTaskInfo>();
	   		    list1 = equipmentOutService.getFlowTaskInfoById(list.get(0).getId());
	   		    list1.get(0).setNextUsersId(","+nextpersonid+",");
	   		    list1.get(0).setNextUsersName(","+nextpersonname+",");
	   		    fti =  list1.get(0);
	   		    equipmentOutService.updateFlowTaskInfo(fti); 			
					
				out.print("{\"result\":\"success\"}");
			    out.flush();
				out.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.print("{\"result\":\"false\"}");
		    out.flush();
			out.close();
		}		
	}
	
	
	
	
	
	
	/**
	 * 申请部门科长审批逻辑处理方法。准备好表单基础信息 及备件信息，跳转到科长审批页面
	 * ------点击流程处理后----从worklist 中获取相关变量
	 * @return  
	 */


	


		
	/**
	 * 申请人代办 处理流程 响应函数
	 * @return
	 */
	public String  finalEnsure(){
		
		String mainid = getRequest().getParameter("mainid");
		getRequest().setAttribute("mainid", mainid);
		
		String  AuditPerson = this.getUserNM();
	    //String  AuditPersonId = this.getUserId();
	    
	    getRequest().setAttribute("AuditPerson", AuditPerson);
    	//获得日期    String 
	    Date now = new Date(); 
	    DateFormat d2 = DateFormat.getDateInstance(); 
	    String timeStr = d2.format(now); 
	    getRequest().setAttribute("timeStr", timeStr);
		
		
		Outstoreform fm = new Outstoreform();
        fm = equipmentOutService.queryOutstoreFormById(mainid);
	       
	    String applyData   = fm.getApplydate();
	    getRequest().setAttribute("applyData", applyData);
	    
	    String  applyDept  = fm.getApplydept();
	    getRequest().setAttribute("applyDept", applyDept);
	    
	    String applyPerson = fm.getApplyperson();	    
	    getRequest().setAttribute("applyPerson", applyPerson);
	    
	    String formSerial = fm.getFormserial();
	    getRequest().setAttribute("formSerial", formSerial);
	    
	   	String outType = fm.getOuttype();
	   	getRequest().setAttribute("outType", outType);
	     
	   	String outReason = fm.getReasonforout();
	   	getRequest().setAttribute("outReason", outReason);
		
	   	String remarks = fm.getRemarks();
	   	getRequest().setAttribute("remarks", remarks);
		
		getRequest().setAttribute("ids", mainid);
		
		String suggestion = fm.getLeadersuggestion();
		getRequest().setAttribute("leadersugg", suggestion);
	
	    String hksuggestion = fm.getHousekeepersuggestion();
		getRequest().setAttribute("hksuggestion", hksuggestion);			
	
		super.initFlowupdateBase();
		return "finalconform";
	}

	/**
	 * 申请人最后确认
	 */
	public void finalEnsureSubmit(){
			
		PrintWriter out = null;
		try {
			out = getResponse().getWriter();	
			String mainid = getRequest().getParameter("mainid");
			
			// 由mainid 获得 outstoreform
			String ensureTime = getRequest().getParameter("ensuretime");
			Outstoreform osf = equipmentOutService.queryOutstoreFormById(mainid);
		//	osf.setAleadonename(applyPerson);
	    	osf.setFinalensuretime(ensureTime);
			equipmentOutService.UpdateOutstoreForm(osf);
						
			//更新备件台账信息  更新  id
			String gridData = this.getRequest().getParameter("gridData");
			String[] arrayData = gridData.split(";");
			for(int i = 0;i<arrayData.length;i++){
				if(!(arrayData[i] == null || arrayData[i].length()<=0)){
					  String[] arr = arrayData[i].split(",");
					  updateEquipmentPartsByOut(arr[0],arr[1]);				  
				}		
			}			
			super.saveUpdateBase("TDM_EPINSTORE", mainid);			
			out.print("{\"result\":\"success\"}");
		    out.flush();
			out.close();		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.print("{\"result\":\"false\"}");
		    out.flush();
			out.close();
		}		
	}
	

	/**
	 * 保存流程发起节点数据。保存到两个表中
	 */
	public void saveData() {		
		PrintWriter out = null ;
		try {
			out = getResponse().getWriter();		
			equipmentOutService.saveToForm(outstoreform); // 在jsp中指定了字段的内容。这里直接存储。
			
			BigDecimal formId = outstoreform.getFormid();
			String flowid = getRequest().getParameter("flowid");
			String flowname = getRequest().getParameter("flowname");			
			String gridData = getRequest().getParameter("gridData");
			//存储下一个节点的人名和id
			
			String nextUserID = getRequest().getParameter("outstoreform.leaderid");
			String nextUserName = getRequest().getParameter("outstoreform.leadername");
			
			
			
			//TODO:解析gridData，遍历存入数据库表。
			String[] arrayData = gridData.split(";");
			for(int i = 0;i<arrayData.length;i++){
				if(!(arrayData[i] == null || arrayData[i].length()<=0)){
					  String[] arr = arrayData[i].split(",");
					  Formrelateequ fq = new Formrelateequ();
					  fq.setFormid(formId);
					  BigDecimal equId = new BigDecimal(arr[0]);
					  BigDecimal equNum=new BigDecimal(arr[1]);
					  String epCode  = arr[2];
					  String epName  = arr [3];
					  String  epGuige  = arr[4];
					  String epAverageprice = arr[5];
					  fq.setEquid(equId);
					  fq.setOutnum(equNum);
					  fq.setepCode(epCode);
					  fq.setepName(epName);
					  fq.setepGuige(epGuige);
					  fq.setepAverageprice(epAverageprice);				  
				      equipmentOutService.saveToForm(fq);
				}		
			}
			super.saveFaqiBase("OUTSTOREFORM",formId.toString());// 参数一，数据表名， 参数二、mainid
			List<FlowTask> list = new ArrayList <FlowTask>();
   		    list = equipmentOutService.getFlowtaskById(formId.toString());  		     
   		    FlowTaskInfo fti = new FlowTaskInfo();
   		    
   		    List<FlowTaskInfo> list1 = new ArrayList <FlowTaskInfo>();
   		    list1 = equipmentOutService.getFlowTaskInfoById(list.get(0).getId());
   		    list1.get(0).setNextUsersId(","+nextUserID+",");
   		    list1.get(0).setNextUsersName(","+nextUserName+",");
   		    fti = list1.get(0);
   		    equipmentOutService.updateFlowTaskInfo(fti); 
			out.print("{\"result\":\"success\"}");
		    out.flush();
			out.close();  		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print("{\"result\":\"false\"}");
		    out.flush();
			out.close();
		}
	}
	/**
	 * 更新设备备件台账信息
	 * @param id
	 * @param outNum
	 */
	public void updateEquipmentPartsByOut(String id,String outNum){
		EquipmentParts eParts = equipmentPartsService.queryEPDataById(id);
		int anum=eParts.getEpActualstock().intValue();
		int onum=Integer.parseInt(outNum);
		int newnum=anum-onum;
		BigDecimal epActualstock=new BigDecimal(newnum);
		eParts.setEpActualstock(epActualstock);
		equipmentPartsService.updateEquipmentParts(eParts);
	}
	
	/**
	 * 验证输入数据的合法性
	 */
	public void validateData(){		
		PrintWriter out = null;	
		try {
			out = getResponse().getWriter();
			String rid = getRequest().getParameter("rid"); // 取 equid
			String num = getRequest().getParameter("inputNum");
		 	//通过id 查询备件库存量 然后对比
			EquipmentParts eParts = equipmentPartsService.queryEPDataById(rid);
			int stockNum = eParts.getEpActualstock().intValue();
			getRequest().setAttribute("stockNum", stockNum); // 想前台传数据
			int inputNum = Integer.parseInt(num);
			if(inputNum<=stockNum){
			out.print("{\"result\":\"success\"}");
			}
			else{
				out.print("{\"result\":\"false\"}");
			}
		    out.flush();
			out.close();		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			out.print("{\"data\":\"false\"}");
		    out.flush();
			out.close();
		}		
	}
	
 /**
  *  退回申请再处理。
  *  public  节点一 ，处理流程 要相应的函数。这个函数是在有回退的情况下才有的响应。-------20150822 
  *  super.saveUpdateExtend("ORIG_RECORD_UPDATE", mainid,actionType,oruShyj);  用于存储审核意见的方法。
 * 
  */ 
	public String applyOut(){

		String mainid = getRequest().getParameter("mainid");
		getRequest().setAttribute("mainid", mainid);
		
		
		//String  AuditPerson = this.getUserName(); 
		String  AuditPerson = this.getUserNM();
	    String  AuditPersonId = this.getUserId();
	    
	    getRequest().setAttribute("AuditPerson", AuditPerson);
    	//获得日期    String 
	    Date now = new Date(); 
	    DateFormat d2 = DateFormat.getDateInstance(); 
	    String timeStr = d2.format(now); 
	    getRequest().setAttribute("timeStr", timeStr);
		
		Outstoreform fm = new Outstoreform();
        fm = equipmentOutService.queryOutstoreFormById(mainid);
	       
	    String applyData   = fm.getApplydate();
	    getRequest().setAttribute("applyData", applyData);
	    
	    String  applyDept  = fm.getApplydept();
	    getRequest().setAttribute("applyDept", applyDept);
	    
	    String applyPerson = fm.getApplyperson();	    
	    getRequest().setAttribute("applyPerson", applyPerson);
	    
	    String formSerial = fm.getFormserial();
	    getRequest().setAttribute("formSerial", formSerial);
	    
	   	String outType = fm.getOuttype();
	   	getRequest().setAttribute("outType", outType);
	     
	   	String outReason = fm.getReasonforout();
	   	getRequest().setAttribute("outReason", outReason);
		
	   	String remarks = fm.getRemarks();
	   	getRequest().setAttribute("remarks", remarks);
	   	
	   	String leadername = fm.getLeadername();
	   	getRequest().setAttribute("leadername", leadername);
	   	
	   	String leaderid = fm.getLeaderid();
	   	getRequest().setAttribute("leaderid", leaderid);
	   	
	   	String hkname = fm.getHousekeepername();
	   	getRequest().setAttribute("hkname", hkname);
	   	String hkid = fm.getHousekeeperid();
	   	getRequest().setAttribute("hkid", hkid);
	   	
		getRequest().setAttribute("ids", mainid);

		BigDecimal  bdflag = fm.getBackflag();
		if(bdflag != null){
			String bflag = bdflag.toString();
			if(bflag.equals("1")){
				getRequest().setAttribute("whoisit","部门科长");
				String suggestion = fm.getLeadersuggestion();
				getRequest().setAttribute("suggestion", suggestion);
				
			}
			else{
				getRequest().setAttribute("whoisit","仓管员");
				String suggestion = fm.getHousekeepersuggestion();
				getRequest().setAttribute("suggestion", suggestion);			
			}
		}
		super.initFlowupdateBase();
		return "apply4out";
            
	}
	/**
	 * 退回的申请单再次提交
	 */
	public void applySubmitted(){
		PrintWriter out = null;
		try {
			out = getResponse().getWriter();				
			//更新outstoreform
			String mainid = getRequest().getParameter("mainid");
//			Outstoreform fm = new Outstoreform();
//	        fm = equipmentOutService.queryOutstoreFormById(mainid);
	        
			String nextUserID = getRequest().getParameter("outstoreform.leaderid");
			String nextUserName = getRequest().getParameter("outstoreform.leadername");
			
	        
			String flowid = getRequest().getParameter("flowid");
			String flowname = getRequest().getParameter("flowname");			
			String gridData = this.getRequest().getParameter("gridData");
			//TODO:解析gridData，遍历存入数据库表。
			String[] arrayData = gridData.split(";");
			for(int i = 0;i<arrayData.length;i++){
				if(!(arrayData[i] == null || arrayData[i].length()<=0)){
					  String[] arr = arrayData[i].split(",");					  
					  BigDecimal equNum=new BigDecimal(arr[1]);
					  List <Formrelateequ> list =  equipmentOutService.queryFormRelateequbyId(mainid,arr[0]); // arr[0] 必须是equid
					  list.get(0).setOutnum(equNum);
					  equipmentOutService.updateFormRelateequ(list.get(0));
				}		
			}
			super.saveUpdateBase("OUTSTOREFORM",mainid.toString());
			
			
			List<FlowTask> list = new ArrayList <FlowTask>();
   		    list = equipmentOutService.getFlowtaskById(mainid);  		     
   		    FlowTaskInfo fti = new FlowTaskInfo();
   		    
   		    List<FlowTaskInfo> list1 = new ArrayList <FlowTaskInfo>();
   		    list1 = equipmentOutService.getFlowTaskInfoById(list.get(0).getId());
   		    list1.get(0).setNextUsersId(","+nextUserID+",");
   		    list1.get(0).setNextUsersName(","+nextUserName+",");
   		    fti = list1.get(0);
   		    equipmentOutService.updateFlowTaskInfo(fti); 
			
			out.print("{\"result\":\"success\"}");
		    out.flush();
			out.close();		
			//String result = super.saveFaqiBase("OUTSTOREFORM",mainid.toString());// 参数一，数据表名， 参数二、mainid
			
		} catch (IOException e) {
			e.printStackTrace();
			out.print("{\"result\":\"false\"}");
		    out.flush();
			out.close();
		}	
	}
}
